/**
 * Independent time operation tool to facilitate subsequent switch to dayjs
 */
import { isDevMode } from "./env";
import { isNumber } from "/@/utils/is";
import dayjs from 'dayjs';

const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm';
const DATE_FORMAT = 'YYYY-MM-DD';

export function formatToDateTime(date: dayjs.Dayjs | undefined = undefined, format = DATE_TIME_FORMAT): string {
  return dayjs(date).format(format);
}

export function formatToDate(date: dayjs.Dayjs | undefined = undefined, format = DATE_FORMAT): string {
  if (!date) {
    return ''
  } else {
    return dayjs(date).format(format);
  }
}

/**
 * 以今天日期推算出，前后第n天的日期
 * 适用于所有查询列表的，日期参数
 * @param date  sub减法，add加法
 * @param num   前或后第几天
 * @return 日期字符窜
 */
export function momentSearchQueryDate(date: string, num?: number) {
  let result = '';
  let begin = 0;
  let end   = 100;
  if (isDevMode()) {
    // 开发环境
    begin = 700;
    end = 100;
  } else {
    // 正式环境
    if (isNumber(num)) {
      begin = num;
      end = num;
    } else {
      begin = 100;
      end = 100;
    }
  }
  if (date == 'sub') {
    result = dayjs().subtract( begin, 'days').format(DATE_FORMAT);
  } else {
    result = dayjs().add( end, 'days').format(DATE_FORMAT);
  }
  return result.trim()
}

export const dateUtil = dayjs;
